package com.lc1993929.actuator.starter.aop;

/**
 * Created by LiuChang on 2017/7/27/027.
 */

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.List;

@Aspect
public class LogMethodInterceptor {
    private Logger logger = LoggerFactory.getLogger(LogMethodInterceptor.class);
    private List<String> exclude;

    public LogMethodInterceptor(String[] exclude) {
        this.exclude = Arrays.asList(exclude);
    }

    @Pointcut(value = "@annotation(com.lc1993929.actuator.starter.annotation.Log)")
    public void annotationLogPointCut() {
    }

    @Around(value = "annotationLogPointCut()")
    public Object logInterceptor(ProceedingJoinPoint joinPoint) throws Throwable {
        String methodName = joinPoint.getSignature().getName();
        if (exclude.contains(methodName)) {
            return joinPoint.proceed();
        }
        logger.info("拦截到：" + methodName);
        long start = System.currentTimeMillis();
        Object result = joinPoint.proceed();
        long end = System.currentTimeMillis();
        logger.info("====method({}), cost({}) ", methodName, (end - start));
        return result;

    }

}
